// pages/user/collect/collect.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    pageloadding:false,
    p:1,
    allStatus: false,
    selectArr: []
  },
  getcollect:function(p){
    let data={token:app.globalData.token,p}
    app.requestJs('user/collect',{data}).then((res)=>{
      if(res.errcode==1){
        let resData=res.data || [];
        this.setData({
          list:data.p==1?resData:[...this.data.list,...resData],
          pageloadding:resData.length>=10?false:true,
          p:data.p
        })
      }else app.utilJs.toast({ title: res.message });
    })
  },
  link:function(e){
    let that=this;
    let shopid=e.currentTarget.dataset.shopid;
    wx.navigateTo({
      url: '/pages/shop/shopinfo/shopinfo?shopid='+shopid,
      events:{
        updateData:function(){
          that.getcollect(1);
        }
      }
    })
  },
  del (e) {
    let shopid = parseInt(e.currentTarget.dataset.shopid)
    this.delcollect(shopid)
  },
  delcollect (id) {
    let ids = []
    if (typeof id === 'number') {
      ids[0] = id
    } else {
      ids = this.data.selectArr
    }
    if (ids.length === 0) {
      wx.showToast({
        title: '请选择要删除的收藏！',
        icon: 'none'
      })
    }
    const data = {
      token: app.globalData.token,
      ids: ids.join()
    }
    app.requestJs("User/delcollect",{data: data, status1: true}).then(() => {
      let list = JSON.parse(JSON.stringify(this.data.list))
      list = list.filter(item => !ids.includes(item.goods_id))
      this.setData({
        list: list
      })
    })
  },
  touchStart: function(e){
    let that = this;
    //开始触摸时 重置所有删除
    that.data.list.forEach(function (v, i) {
        if (v.isTouchMove) v.isTouchMove = false; // 只操作为true的
    })
    // 记录手指触摸开始坐标
    that.setData({
        startX: e.changedTouches[0].clientX,  // 开始X坐标
        startY: e.changedTouches[0].clientY,  // 开始Y坐标
        list: that.data.list
    })
  },
  touchMove: function(e){
    let that = this,
        index = e.currentTarget.dataset.index,    // 当前下标
        startX = that.data.startX,                // 开始X坐标
        startY = that.data.startY,                // 开始Y坐标
        touchMoveX = e.changedTouches[0].clientX, // 滑动变化坐标
        touchMoveY = e.changedTouches[0].clientY, // 滑动变化坐标
        // 获取滑动角度
        angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });// 判断滑动角度
    that.data.list.forEach(function (v, i) {
      v.isTouchMove = false
      // 滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        // 右滑
        if (touchMoveX > startX) 
          v.isTouchMove = false
        // 左滑
        else 
          v.isTouchMove = true
      }
    })
    // 更新数据
    that.setData({
        list: that.data.list
    })
  },
  // 计算滑动角度
  angle: function (start, end) {
    let that = this,
        _X = end.X - start.X,
        _Y = end.Y - start.Y;
    // 返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  all () {
    let list = JSON.parse(JSON.stringify(this.data.list))
    this.data.selectArr = []
    for (let index = 0; index < list.length; index++) {
      const element = list[index]
      element.checked = !this.data.allStatus
      if (!this.data.allStatus) {
        this.data.selectArr.push(element.goods_id)
      }
    }
    this.setData({
      list: list,
      allStatus: !this.data.allStatus
    })
  },
  stop (e) {
    let index = e.currentTarget.dataset.index
    if (this.data.list[index]?.checked === undefined) {
      this.data.list[index].checked = false
    }
    this.setData({
      [`list[${index}].checked`]: !this.data.list[index].checked
    })
  },
  collectCheckbox (e) {
    let value = e.detail.value
    value = value.map(item => {
      return parseInt(item)
    })
    this.data.selectArr = value
    this.setData({
      allStatus: this.data.list.length === value.length
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 测试用
    // this.setData({
    //   list: [
    //     {
    //       goods_id: 1,
    //       thumb: '/img/61.png',
    //       goods_name: '海南贵妃芒果当季热带新鲜水果',
    //       price: '19.90'
    //     },{
    //       goods_id: 2,
    //       thumb: '/img/61.png',
    //       goods_name: '海南贵妃芒果当季热带新鲜水果',
    //       price: '19.90'
    //     }
    //   ]
    // })
    // return
    this.getcollect(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("分页啦");
    if(this.data.pageloadding)return
    let p=this.data.p+1;
    this.getcollect(p);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})